package com.xand.bt.terminal.btce.connector;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;

import com.xand.bt.terminal.btce.connector.market.MarketReference;
import com.xand.bt.terminal.btce.connector.market.MarketStore;
import com.xand.bt.terminal.common.api.MarketConnector;
import com.xand.bt.terminal.common.api.MarketDepthUpdate;
import com.xand.bt.terminal.common.api.TickerUpdate;

public class BTCEMarketConnector implements MarketConnector {

	private ConnectorConfig connectorConfig;

	private MarketStore marketStore;

	private MarketConnectorThread connectorThread;

	public BTCEMarketConnector() {
		this.connectorConfig = new ConnectorConfig();
		this.marketStore = new MarketStore();
	}

	public String getName() {
		return "BTC-e market connector";
	}

	public void init() {

		String[] references = this.connectorConfig.getReferences();
		for(String reference : references) {
			MarketReference marketReference = new MarketReference(reference);
			this.marketStore.addMarketReference(marketReference);
		}

		this.connectorThread = new MarketConnectorThread(this.connectorConfig, this.marketStore);
		this.connectorThread.start();

	}

	public void setConfiguration(Properties props) {

		this.connectorConfig.setPassword(props.getProperty("password"));
		this.connectorConfig.setUsername(props.getProperty("username"));

		String referencesProp = props.getProperty("references");
		StringTokenizer st = new StringTokenizer(referencesProp, ";");
		String references[] = new String[st.countTokens()];
		int i = 0;
		while(st.hasMoreTokens()) {
			references[i] = st.nextToken();

			i++;
		}

		this.connectorConfig.setReferences(references);

		Map<String, String> depth = new HashMap<String, String>();
		Map<String, String> ticker = new HashMap<String, String>();
		Map<String, String> trades = new HashMap<String, String>();
		for(String reference : references) {

			String depthUrl = props.getProperty("depth_url." + reference);
			String tickerUrl = props.getProperty("ticker_url." + reference);
			String tradesUrl = props.getProperty("trades_url." + reference);

			depth.put(reference, depthUrl);
			ticker.put(reference, tickerUrl);
			trades.put(reference, tradesUrl);

		}

		this.connectorConfig.setDepthUrl(depth);
		this.connectorConfig.setTickerUrl(ticker);
		this.connectorConfig.setTradesUrl(trades);

	}

	public void updateMarketDepth(List<MarketDepthUpdate> updateList) {

		// populate updates
		List<MarketDepthUpdate> updates = this.marketStore.getCurrentUpdates();

		for(MarketDepthUpdate currentUpdate : updates) {
			updateList.add(currentUpdate);
		}

	}

	public void updateTicker(TickerUpdate tickerUpdate) {

	}

	public void updateOrders() {

	}

	public void placeOrder() {

	}

	public void cancelOrder() {

	}

	public void login() {

	}

	public void disconnect() {

	}

	public void stop() {

	}

	public List<String> getMarketReferences() {
		String[] references = this.connectorConfig.getReferences();
		
		List<String> ret = new ArrayList<String>();
		for(String reference : references) {
			ret.add(reference);
		}
		
		return ret;
	}

}
